package com.wang.decagency.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.decagency.entity.GCost;
import com.wang.decagency.entity.GDeclaration;
import com.wang.decagency.entity.SysUser;
import com.wang.decagency.service.impl.GCostServiceImpl;
import com.wang.utils.ResultEntity;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wang
 * @since 2023-05-29
 */
@Slf4j
@RestController
@Api(tags = "费用管理")
@RequestMapping("/decagency/cost")
public class GCostController {

    @Autowired
    GCostServiceImpl costService;

    @PostMapping("/getCostList")
    @ApiOperation(value = "查询费用列表", notes = "查询费用列表")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功")})
    public Object getCostList(@RequestBody Map<String, Object> mapParam) throws InvocationTargetException, IllegalAccessException {
        GCost gCost = JSON.parseObject(JSON.toJSONString(mapParam), GCost.class);
        log.info("gCost:"+JSON.toJSONString(gCost));
        // 从params中获取分页参数
        int pageNum = mapParam.get("pageNum")!=null ? (int) mapParam.get("pageNum"):1 ;
        int pageSize =mapParam.get("pageSize")!=null ? (int) mapParam.get("pageSize"):5 ;

        log.info(JSON.toJSONString("pageNum:"+pageNum));
        QueryWrapper<GCost> queryWrapper = new QueryWrapper<>();
        if (gCost.getOrderNo() != null) {
            queryWrapper.like("order_no", gCost.getOrderNo());
        }
        if (gCost.getType() != null) {
            queryWrapper.like("type", gCost.getType());
        }
        Page<GCost> page = new Page<>(pageNum, pageSize);
        IPage<GCost> SysUserList = costService.page(page, queryWrapper);
        return ResultEntity.ok(SysUserList);
    }



    @PostMapping("/addCost")
    @ApiOperation(value = "添加报关单", notes = "添加报关单")
    public Object addCost(@RequestBody @ApiParam(name = "cost", value = "报关单信息", required = true) GCost cost) {
        if (Objects.isNull(cost)  || cost.getOrderNo() == null) {
            return ResultEntity.fail("入参不全");
        }
        costService.saveOrUpdate(cost);
        return ResultEntity.ok();
    }

    @PostMapping("/getByKey")
    @ApiOperation(value = "查询详情", notes = "查询详情")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功")})
    public Object getByKey(@RequestBody @ApiParam(name = "cost", value = "报关单信息") GCost cost) throws Exception {
        cost = costService.getById(cost.getCostId());
        return ResultEntity.ok(cost);
    }

    @PostMapping("/delByKeys")
    @ApiOperation(value = "根据主键删除", notes = "根据主键删除")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功")})
    public Object delByKeys(@RequestBody List<Integer> ids) throws Exception {
        boolean b = costService.removeByIds(ids);
        return ResultEntity.ok(b);
    }



}
